﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;

namespace SIPO.Web.Forms.Administration.Catalog
{

   public partial class recDebtorform : BasePage
   {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {
            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (!Page.IsPostBack)
            {
                recRegionRepo recRegionDal = new recRegionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recCityRepo recCityDal = new recCityRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recDebtorRepo recDebtorDals = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                List<recRegion> regions = recRegionDal.GetList();
                ddlRegion.Items.Clear();
                regions.ForEach(delegate(recRegion region)
                {
                    ddlRegion.Items.Add(new ListItem(region.RegionCode + " - " + region.RegionName, region.RegionID.ToString()));
                });

                ddlRegion.Items.Insert(0, new ListItem("-- pilih wilayah --", string.Empty));
                txtCode.Text =  recDebtorDals.generatecode(false).ToString();
              

                if (id > 0) // Edit an existing item
                {
                    ChkIsPerson.Enabled = false;
                    recDebtorRepo recDebtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recDebtor recDebtor = recDebtorDal.GetById(id);
                    if (recDebtor != null)
                    {
                        try
                        {
                            ddlRegion.Items.FindByValue(recDebtor.RegionID.ToString()).Selected = true;
                        }
                        catch
                        {
                            ddlRegion.SelectedIndex = 0;
                        }
                        ddlCity.Items.Clear();
                        ddlCity.Items.Insert(0, new ListItem("-- pilih kota --", string.Empty));
                        if (ddlRegion.SelectedValue.ToString() != "")
                        {
                            List<recCity> cities = recCityDal.GetList().FindAll(x=> x.RegionID == (int) recDebtor.RegionID);
                           
                            cities.ForEach(delegate(recCity city)
                            {
                                ddlCity.Items.Add(new ListItem(city.CityID + " - " + city.CityName, city.CityID.ToString()));
                            });
                           
                        }
                        try
                        {
                                ddlCity.Items.FindByValue(recDebtor.CityID.ToString()).Selected = true;
                           
                        }
                        catch { ddlCity.SelectedIndex = 0; }
                        hfID.Value = id.ToString();
                        txtCode.Text = recDebtor.DebtorCode;
                        txtName.Text = recDebtor.DebtorName;
                        txtAddress1.Text = recDebtor.Address1;
                        txtAddress2.Text = recDebtor.Address2;
                        txtAddress3.Text = recDebtor.Address3;
                        txtEmail.Text = recDebtor.Email;
                        txtFax.Text = recDebtor.Fax;
                        txtNPWP.Text = recDebtor.NPWP;
                        txtPhone.Text = recDebtor.Phone;
                        txtWebsite.Text = recDebtor.Website;
                        txtZipCode.Text = recDebtor.ZipCode;
                        txtCode.Enabled = false;
                       
                           
                        
                        chkIsActive.Checked = recDebtor.IsActive ? recDebtor.IsActive : false;
                        ChkIsPerson.Checked =  recDebtor.IsIndividual ? recDebtor.IsIndividual : false ;
                    }
                }
                GetPrivilege();
            }
        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
}

}
